package com.ruoyi.business.service.impl;

import java.util.List;

import com.ruoyi.business.utils.ConstPools;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.domain.vo.InnerObjVo;
import com.ruoyi.system.mapper.SysRoleMapper;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.impl.SysUserServiceImpl;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.TrainManMapper;
import com.ruoyi.business.domain.TrainMan;
import com.ruoyi.business.service.ITrainManService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 教练Service业务层处理
 * 
 * @author 吴倩芸
 * @date 2023-03-05
 */
@SuppressWarnings("ALL")
@Service
public class TrainManServiceImpl extends ServiceImpl<TrainManMapper, TrainMan> implements ITrainManService
{
    @Autowired
    private TrainManMapper trainManMapper;

    @Autowired
    private ISysRoleService roleService;

    @Autowired
    private SysUserRoleMapper userRoleMapper;

    /**
     * 查询教练
     * 
     * @param id 教练主键
     * @return 教练
     */
    @Override
    public TrainMan selectTrainManById(Long id)
    {
        return trainManMapper.selectTrainManById(id);
    }

    /**
     * 查询教练列表
     * 
     * @param trainMan 教练
     * @return 教练
     */
    @Override
    public List<TrainMan> selectTrainManList(TrainMan trainMan)
    {
        return trainManMapper.selectTrainManList(trainMan);
    }

    /**
     * 新增教练
     * 
     * @param trainMan 教练
     * @return 结果
     */
    @Override
    public int insertTrainMan(TrainMan trainMan)
    {
        if (trainMan.getUserId() == null) {
            throw new RuntimeException("请先选择用户id");
        }
        trainMan.setId(Long.toString(trainMan.getUserId()));
        // 将用户角色修改
        // TODO：暂时没有什么好点子，先硬编码了
        List<Long> sysRoles = roleService.selectRoleListByUserId(trainMan.getUserId());
        for (Long roleId : sysRoles) {
            if (roleId == ConstPools.TRAIN_MAN_ROLE_ID) {
                throw new RuntimeException("教练不可重复申请");
            }
        }
        roleService.insertAuthUsers(ConstPools.TRAIN_MAN_ROLE_ID, new Long[]{trainMan.getUserId()});
        return trainManMapper.insertTrainMan(trainMan);
    }

    /**
     * 修改教练
     * 
     * @param trainMan 教练
     * @return 结果
     */
    @Override
    public int updateTrainMan(TrainMan trainMan)
    {
        return trainManMapper.updateTrainMan(trainMan);
    }

    /**
     * 批量删除教练
     * 
     * @param ids 需要删除的教练主键
     * @return 结果
     */
    @Override
    public int deleteTrainManByIds(Long[] ids)
    {
        return trainManMapper.deleteTrainManByIds(ids);
    }

    /**
     * 删除教练信息
     * 
     * @param id 教练主键
     * @return 结果
     */
    @Override
    public int deleteTrainManById(Long id)
    {
        return trainManMapper.deleteTrainManById(id);
    }

    @Override
    public List<TrainMan> selectHighStarList(Integer limit) {
        return trainManMapper.selectHighStarList(limit);
    }

    @Override
    public List<InnerObjVo> queryRefTrainManInnerObj(String name) {
        return trainManMapper.queryRefTrainManInnerObj(name);
    }
}
